package com.ywq.app.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.ArtFindBiz;
import com.ywq.app.biz.ArtMadeBiz;
import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.PwSchool;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.common.constant.Constant;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;

@Controller
public class ArtFindMiscController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private ArtFindBiz artFindBiz;
	@Autowired
	private ArtMadeBiz artMadeBiz;
	@Autowired
	private ArtPicBiz artPicBiz;
	/**
	 * 发现列表页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artFind/artistList.htm")
	public String artRoom(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artFind/artFind";
			}
	//查询所有属于发现的艺术家
	@RequestMapping(value = "/misc/artFind/artFindGrid.htm")
	public String artMadeGrid(Integer page,Integer pagesize, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModel = artFindBiz.getArtistListForMisc(page,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModel));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtistList failure.", e);
			}
		}
		return null;
	}
	//发现添加艺术家
	@RequestMapping(value = "/misc/artFind/addFindArtistWeb.htm")
	public String addArtistWeb(HttpServletRequest request, HttpServletResponse response) throws IOException {
			request.setAttribute("have", "no");
			return "misc/artFind/addFindArtistWeb";
	}
	//联想公司名称
	@RequestMapping(value = "/misc/company/lxCompany.htm")
	public String lxCompany(HttpServletRequest request, HttpServletResponse response,
			String name) throws IOException {
		try {
			response.setContentType("text/html; charset=UTF-8"); 
        	PrintWriter out = response.getWriter();
			List<PwSchool> companys = artFindBiz.getCompanyByName(name);
			out.print(JSONArray.fromObject(companys));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//添加艺术家
	@RequestMapping(value = "/misc/artFind/insertArtist.htm", method = RequestMethod.POST)
	public String insertArtist(HttpServletRequest request, HttpServletResponse response, ArtArtist artArtist) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			//添加艺术家 //同时生成一个登陆账号
			artFindBiz.insertArtArtist(artArtist);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtist failure.", e);
			}
		}
		return null;
		
	}
	//修改发现艺术家
	@RequestMapping(value = "/misc/artFind/updateFindArtistWeb.htm")
	public String upadteArtistWeb(String artistId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			 try {
				ArtArtist artArtist =artMadeBiz.getArtist(artistId);
				if(artArtist.getSchoolid()!="" && !"".equals(artArtist.getSchoolid())){
					PwSchool pwSchool =artFindBiz.getSchool(artArtist.getSchoolid());
					artArtist.setSchoolName(pwSchool.getName());
				}
				request.setAttribute("have", "have");
				request.setAttribute("artArtist", artArtist);
				} catch (AppBusinessException e) {
					e.printStackTrace();
				}
			return "misc/artFind/addFindArtistWeb";
			}
	@RequestMapping(value = "/misc/artFind/addFindArtPicWeb.htm")
	public String addArtPicWeb(String classId,String type,HttpServletRequest request,HttpServletResponse response) throws IOException {
		//classId 艺术家Id
		request.setAttribute("classId", classId);
		try {
			ArtArtist artArtist =artMadeBiz.getArtist(classId);
			request.setAttribute("type", type);
			if("find".equals(type)){
				request.setAttribute("classType", Constant.PIC_FIND_CLASS);
				request.setAttribute("hotType", Constant.PIC_FIND_HOT);
				request.setAttribute("profType", Constant.PIC_FIND_PROF);
			}
			if(artArtist!=null){
				request.setAttribute("artName", artArtist.getName());
				request.setAttribute("artistId", artArtist.getId());
			}
			//查询所有艺术家
			List<ArtArtist> artist =artMadeBiz.getArtistListForFind();
			request.setAttribute("artist",artist);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		request.setAttribute("have", "no");
		return "misc/artFind/addFindArtPicWeb";
	}
	//所有分类页面
	@RequestMapping(value = "/misc/artFind/allClass.htm")
	public String allClass(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artFind/allClass";
			}
	@RequestMapping(value = "/misc/artFind/allClassGrid.htm")
	public String allClassGrid(Integer page,Integer pagesize, String name,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModel = artPicBiz.getAllClassPageModel(page,pagesize,name);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModel));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtistList failure.", e);
			}
		}
		return null;
	}
	
}
